//
//  LGMainController.m
//  微博oc版
//
//  Created by Apple_Lzzy27 on 16/10/10.
//  Copyright © 2016年 Apple_Lzzy27. All rights reserved.
//

#import "LGMainController.h"
#import "CZAdditions.h"
#import "LGNavigationController.h"
#import "LGBaisController.h"
#import "LGAuthorization.h"
@interface LGMainController ()
@property(nonatomic, strong) NSTimer *time;
@end

@implementation LGMainController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self controllers];
    //接收通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userLogin) name:UserLoginNitification object:nil];
    [self setTabbarPagValue];
}

- (void)controllers{
    [self setDict];
    [self addWritingButton];
    
}
//添加字典数据

-(void)setDict{
    //创建一个字典visitordiscover_image_message   visitordiscover_image_profile
    NSArray *array = @[@{@"className":@"LGHomeController",@"title":@"首页",@"imageName":@"home",
                         @"visitor":@{@"icon":@"",@"text":@""}},
                       @{@"className":@"LGMessageController",@"title":@"消息",@"imageName":@"message_center",
                         @"visitor":@{@"icon":@"visitordiscover_image_profile",@"text":@"登陆后，你就可以评论别人的微博"}},
                        @{@"className":@"",@"title":@"viewController"},
                       @{@"className":@"LGDiscoverController",@"title":@"发现",@"imageName":@"discover",
                         @"visitor":@{@"icon":@"visitordiscover_image_message",@"text":@"登录后你就发现"}},
                       @{@"className":@"LGBaisController",@"title":@"我",@"imageName":@"profile",
                         @"visitor":@{@"icon":@"visitordiscover_image_profile",@"text":@"登录后就能看到你的信息了"}}];
    //创建一个保存控制器的可变数组
    NSMutableArray *arrayM = [NSMutableArray array];
    
    for (int i = 0; i<array.count; i++) {
    NSDictionary *dict = array[i];
   [arrayM addObject:[self setChildcontrollers:dict]];
    }
    //设置控制器
    self.viewControllers = arrayM;
}


/**
 *  添加子控制器
 */
- (UIViewController *)setChildcontrollers:(NSDictionary *)dict{
    
    NSString *clasName = dict[@"className"];
    
    NSString *title = dict[@"title"];
    NSString *image = dict[@"imageName"];
    NSString *normalName = [NSString stringWithFormat:@"tabbar_%@",image];
    NSString *selecedImageName = [NSString stringWithFormat:@"%@_selected",normalName];
    NSDictionary *visitor = dict[@"visitor"];
    //字符串转类
    if ([clasName  isEqual: @""]) {
        return [[UIViewController alloc] init];
    }
    Class  cla  = NSClassFromString(clasName);
//设置控制器的一些属性
    LGBaisController *vc = [[cla alloc] init];
    vc.title = title;
    vc.visitorDictionary = visitor;
    vc.tabBarItem.image = [[UIImage imageNamed:normalName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:selecedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    NSDictionary *attr = @{NSForegroundColorAttributeName:[UIColor orangeColor]};
    [vc.tabBarItem setTitleTextAttributes:attr forState:UIControlStateSelected];
    LGNavigationController *nav = [[LGNavigationController alloc] initWithRootViewController:vc];
    
    
   return nav;
    
}
//添加拽写按钮
- (void)addWritingButton{
    
    UIButton *writButn = [UIButton cz_imageButton:@"tabbar_compose_icon_add" backgroundImageName:@"tabbar_compose_button"];
    
    [self.tabBar addSubview:writButn];
    CGFloat btnW = LGScreenW / self.viewControllers.count;
    
    //insetBy
    writButn.frame = CGRectInset(self.tabBar.bounds, btnW*2, 0);
    
    
}
- (void)setTabbarPagValue{
    self.time = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(timeBag) userInfo:nil repeats:YES];
    
   }

- (void)timeBag{
    
    [[LGNetWorkingManager shard] unreadCount:^(int count) {
        NSString * vaule = count <= 0 ? nil:[NSString stringWithFormat:@"%d",count];
        NSLog(@"有%@条消息未读",vaule);
        self.tabBar.items[0].badgeValue = vaule;
        [UIApplication sharedApplication].applicationIconBadgeNumber = [vaule integerValue];
        
    }];
    

    
}
/**
 *  注销事件
 */
- (void)dealloc
{   //关闭定时器
    [self.time invalidate];
    //移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma  mark 通知事件

-(void)userLogin{
    
    NSLog(@"通知登录");
    LGAuthorization *autho = [[LGAuthorization alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:autho];
    
    
    [self presentViewController:nav animated:YES completion:nil];
    
    
}


@end
